﻿Public Class DRUGAGENTCVF
    Dim connect As CONDB = CONDB.NewConnection()
    'Dim connectlocal As conlocal = conlocal.NewConnection
    Dim PROVIDER_Check As New PROVIDERSCLASS

    '---------clear text&tag when click
    Private Sub AGENTCODETxt_Click(sender As Object, e As EventArgs) Handles AGENTCODETxt.Click
        AGENTCODETxt.Text = ""
        AGENTCODETxt.Tag = ""
    End Sub

    Private Sub PRDCODETxt_Click(sender As Object, e As EventArgs) Handles PRDCODETxt.Click
        PRDCODETxt.Text = ""
        PRDCODETxt.Tag = ""
    End Sub

    Private Sub UNITIDTxt_Click(sender As Object, e As EventArgs) Handles UNITIDTxt.Click
        UNITIDTxt.Text = ""
        UNITIDTxt.Tag = ""
    End Sub

    Private Sub FREEUNITIDTxt_Click(sender As Object, e As EventArgs) Handles EXTRAUNITTxt.Click
        EXTRAUNITTxt.Text = ""
        EXTRAUNITTxt.Tag = ""
    End Sub

    '----------auto query unit when select product--------------
    Private Sub PRDCODETxt_TextChanged(sender As Object, e As EventArgs) Handles PRDCODETxt.TextChanged
        Dim unitid As Integer
        Dim unit As String

        Try
            unitid = connect.GetTable("SELECT B_UNIT FROM masproduct WHERE PRDCODE = '" & PRDCODETxt.Tag & "'").Rows(0)(0)
            unit = connect.GetTable("SELECT UNITNAME_TH FROM masunit WHERE UNITID = '" & unitid & "'").Rows(0)(0)

            UNITIDTxt.Text = unit
            UNITIDTxt.Tag = unitid
        Catch ex As Exception
            UNITIDTxt.Text = ""
            UNITIDTxt.Tag = ""
        End Try
    End Sub

    Public Sub clearForm()
        ACVFIDTxt.Text = ""
        AGENTCODETxt.Text = ""
        AGENTCODETxt.Tag = ""
        PRDCODETxt.Text = ""
        PRDCODETxt.Tag = ""
        UNITIDTxt.Text = ""
        UNITIDTxt.Tag = ""
        AGENTPRCTxt.Text = ""
        CVFTxt.Text = ""
        DEALQUANTxt.Text = ""
        EXTRAQUANTxt.Text = ""
        EXTRAUNITTxt.Text = ""
        EXTRAUNITTxt.Tag = ""
        DISCOUNTTxt.Text = ""
        SEQTxt.Text = ""
        COMMENTTxt.Text = ""
        agentcvfStatusChk.Checked = False
    End Sub

    Private Sub DRUGAGENTCVF_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        selectAgentcvfGrid()

        filterAgent()
        filterProduct()
        filterUnit()
        filterFreeUnit()

        DesignForm(Me)
        DesignGrid(drugagentcvfGrid)

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Public Sub selectAgentcvfGrid()
        Dim objSelectAgentcvf As New DRUGAGENTCVFCLASS
        Dim DRUGAGENTCVF As DataTable
        DRUGAGENTCVF = connect.GetTable(objSelectAgentcvf.selectAgentCVF_)
        drugagentcvfGrid.PrimaryGrid.DataSource = DRUGAGENTCVF
    End Sub

    '-----------หนูน้อยอะไรหว่า----------------
    Public Sub filterAgent()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(agentcode AS CHAR(8)) AS AGENTCODE, company_name FROM agent WHERE agentype <> ''"
        s = New FILTERCLASS(AGENTCODETxt, sql, "รหัสผู้ค้า,ชื่อผู้ค้า", "50,160", "1,1", "1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    Public Sub filterProduct()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(prdcode AS CHAR(8)) AS PRDCODE, prdname FROM masproduct WHERE prdcat = '1' OR prdcat = '2'"
        s = New FILTERCLASS(PRDCODETxt, sql, "รหัสผลิตภัณฑ์,ชื่อผลิตภัณฑ์", "50,160", "1,1", "1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    Public Sub filterUnit()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(unitid AS CHAR(8)) AS UNITID, unitname_th, unitname_en FROM masunit"
        s = New FILTERCLASS(UNITIDTxt, sql, "รหัสหน่วยนับ,หน่วยนับ(ไทย),หน่วยนับ(Eng)", "50,160,160", "1,1,1", "1,1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    Public Sub filterFreeUnit()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(unitid AS CHAR(8)) AS UNITID, unitname_th, unitname_en FROM masunit"
        s = New FILTERCLASS(EXTRAUNITTxt, sql, "รหัสหน่วยนับ,หน่วยนับ(ไทย),หน่วยนับ(Eng)", "50,160,160", "1,1,1", "1,1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True
    End Sub

    Private Sub DrugUseTemplateGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles drugagentcvfGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow

        Try
            ACVFIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสสินค้าที่ขายโดยผู้ค้า").Value()
        Catch ex As Exception
            ACVFIDTxt.Text = ""
        End Try

        Try
            AGENTCODETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อผู้ค้า").Value()
            AGENTCODETxt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสผู้ค้า").Value()
        Catch ex As Exception
            AGENTCODETxt.Text = ""
            AGENTCODETxt.Tag = ""
        End Try

        Try
            PRDCODETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อผลิตภัณฑ์").Value()
            PRDCODETxt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสผลิตภัณฑ์").Value()
        Catch ex As Exception
            PRDCODETxt.Text = ""
            PRDCODETxt.Tag = ""
        End Try

        Try
            UNITIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("หน่วยนับ").Value()
            UNITIDTxt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสหน่วยนับ").Value()
        Catch ex As Exception
            UNITIDTxt.Text = ""
            UNITIDTxt.Tag = ""
        End Try

        Try
            AGENTPRCTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ราคา").Value()
        Catch ex As Exception
            AGENTPRCTxt.Text = ""
        End Try

        Try
            CVFTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ขนาดบรรจุ").Value()
        Catch ex As Exception
            CVFTxt.Text = ""
        End Try

        Try
            DEALQUANTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("จำนวนที่ได้ของแถม").Value()
        Catch ex As Exception
            DEALQUANTxt.Text = ""
        End Try

        Try
            EXTRAQUANTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("จำนวนแถม").Value()
        Catch ex As Exception
            EXTRAQUANTxt.Text = ""
        End Try

        Try
            EXTRACVFTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ขนาดบรรจุ(แถม)").Value()
        Catch ex As Exception
            EXTRACVFTxt.Text = ""
        End Try

        Try
            EXTRAUNITTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("หน่วยของแถม").Value()
            EXTRAUNITTxt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสหน่วยของแถม").Value()
        Catch ex As Exception
            EXTRAUNITTxt.Text = ""
            EXTRAUNITTxt.Tag = ""
        End Try

        Try
            DISCOUNTTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ส่วนลด").Value()
        Catch ex As Exception
            DISCOUNTTxt.Text = ""
        End Try

        Try
            SEQTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ลำดับการซื้อ").Value()
        Catch ex As Exception
            SEQTxt.Text = ""
        End Try

        Try
            COMMENTTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("หมายเหตุ").Value()
        Catch ex As Exception
            COMMENTTxt.Text = ""
        End Try

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            agentcvfStatusChk.Checked = True
        Else
            agentcvfStatusChk.Checked = False
        End If

        Dim objChkboxUpdate As New CLICKGRIDCHECKBOX
        objChkboxUpdate.clickChkboxUpdate("agentcvf", CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value(), "STATUS", "ACVFID", ACVFIDTxt.Text)
    End Sub

    Private Sub addAgentcvf_Click(sender As Object, e As EventArgs) Handles addAgentcvf.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If AGENTCODETxt.Text.Length.ToString > 0 And PRDCODETxt.Text.Length.ToString > 0 And UNITIDTxt.Text.Length.ToString > 0 And CVFTxt.Text.Length.ToString And DEALQUANTxt.Text.Length.ToString > 0 And EXTRAQUANTxt.Text.Length.ToString > 0 And DISCOUNTTxt.Text.Length.ToString > 0 And AGENTPRCTxt.Text.Length.ToString > 0 Then
                    'connectlocal = conlocal.NewConnection
                    Dim objInsertData As New DRUGAGENTCVFCLASS
                    objInsertData.AGENTCODE_ = AGENTCODETxt.Tag
                    objInsertData.PRDCODE_ = PRDCODETxt.Tag
                    objInsertData.UNITID_ = UNITIDTxt.Tag
                    objInsertData.AGENTPRC_ = AGENTPRCTxt.Text
                    objInsertData.CVF_ = CVFTxt.Text
                    objInsertData.DEALQUAN_ = DEALQUANTxt.Text
                    objInsertData.EXTRAQUAN_ = EXTRAQUANTxt.Text
                    objInsertData.EXTRACVF_ = EXTRACVFTxt.Text
                    objInsertData.EXTRAUNIT_ = EXTRAUNITTxt.Tag
                    objInsertData.DISCOUNT_ = DISCOUNTTxt.Text
                    objInsertData.SEQ_ = SEQTxt.Text
                    objInsertData.COMMENT_ = COMMENTTxt.Text
                    objInsertData.STATUS_ = agentcvfStatusChk.Checked

                    If objInsertData.insertAgentCVF() = True Then
                        selectAgentcvfGrid()
                        clearForm()
                    End If

                    connect.Dispose()
                Else
                    MsgBox("ท่านต้องทำการกรอกข้อมูลให้ครบถ้วนทุกช่องก่อนทำการเพิ่มข้อมูล")
                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editAgentcvf_Click(sender As Object, e As EventArgs) Handles editAgentcvf.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                If ACVFIDTxt.Text.Length.ToString > 0 And AGENTCODETxt.Text.Length.ToString > 0 And PRDCODETxt.Text.Length.ToString > 0 And UNITIDTxt.Text.Length.ToString > 0 And CVFTxt.Text.Length.ToString And DEALQUANTxt.Text.Length.ToString > 0 And EXTRAQUANTxt.Text.Length.ToString > 0 And DISCOUNTTxt.Text.Length.ToString > 0 And AGENTPRCTxt.Text.Length.ToString > 0 Then
                    Dim editStat As Integer
                    editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลสินค้าที่ขายโดยผู้ค้าดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

                    If editStat = 1 Then
                        'connectlocal = conlocal.NewConnection
                        Dim objEditData As New DRUGAGENTCVFCLASS
                        objEditData.ACVFID_ = ACVFIDTxt.Text
                        objEditData.AGENTCODE_ = AGENTCODETxt.Tag
                        objEditData.PRDCODE_ = PRDCODETxt.Tag
                        objEditData.UNITID_ = UNITIDTxt.Tag
                        objEditData.AGENTPRC_ = AGENTPRCTxt.Text
                        objEditData.CVF_ = CVFTxt.Text
                        objEditData.DEALQUAN_ = DEALQUANTxt.Text
                        objEditData.EXTRAQUAN_ = EXTRAQUANTxt.Text
                        objEditData.EXTRACVF_ = EXTRACVFTxt.Text
                        objEditData.EXTRAUNIT_ = EXTRAUNITTxt.Tag
                        objEditData.DISCOUNT_ = DISCOUNTTxt.Text
                        objEditData.SEQ_ = SEQTxt.Text
                        objEditData.COMMENT_ = COMMENTTxt.Text
                        objEditData.STATUS_ = agentcvfStatusChk.Checked

                        If objEditData.editAgentCVF() = True Then
                            selectAgentcvfGrid()
                            clearForm()
                        End If

                        connect.Dispose()
                    End If
                Else
                    MsgBox("ท่านต้องทำการกรอกข้อมูลให้ครบถ้วนก่อนทำการแก้ไขข้อมูล")
                End If
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL("agentcvf")
        nextform.ShowDialog()
    End Sub

    Private Sub exportExcel_Click(sender As Object, e As EventArgs) Handles exportExcel.Click
        Dim nextform As New IMPORTEXCEL("agentcvf")
        nextform.ShowDialog()
    End Sub


End Class